130 REM TO SORT NUMERICALLY, SIMPLY OMIT THE $-SIGN ON THE ARRAY A( )
140 REM THAT REQUIRES 4 CHANGES IN 3 LINES!!
150 REM INITIALIZE
160 DIM A$(200),B%(200): REM A$( HOLDS PART TO BE SORTED, B%( THE RESULTING SORT VECTORS. INTEGER VLBS RO REDUCE SPACE NEEDS
170 TEXT : HOME
180 D$ = CHR$(4):ESC$ = CHR$(27)
190 PRINT "** COPYRIGHT 1983 BY MICROSPARC, INC. **": VTAB 3: PRINT "VISICALC FILE SORTER"
200 PRINT "BY COLIN C. GRAHAM"
210 PRINT "MAY 1983"
220 PRINT
230 PRINT "ENTER SOURCE FILE NAME (VC P-TO-DISK):"
240 INPUT INNAME$
250 PRINT "IS ";INNAME$;" OK?"
260 GET A$: IF A$ < >"Y" THEN 230
270 PRINT : PRINT "ENTER OUTFILE NAME:"
280 INPUT FILE$
290 PRINT "IS ";FILE$;" OK?"
300 GET A$: IF A$ < >"Y" THEN 270
310 PRINT : PRINT "WHICH COLUMN IS BEING SORTED?"
320 INPUT COL$
330 PRINT "IS ";COL$;" OK?"
340 GET A$: IF A$ < >"Y" THEN 310
350 PRINT : PRINT "ENTER NUMBER OF FIRST ROW WRITTEN TO": PRINT "FILE (0-200)"
360 INPUT R1
370 PRINT "IS ";R1;" OK?"
380 GET A$: IF A$ < >"Y" THEN 350
390 REM OPEN SOURCE FILE & CHECK PARAMETERS
400 PRINT D$;"OPEN ";INNAME$
410 PRINT D$;"READ ";INNAME$
420 ONERR GOTO 460
430 J = 1
440 INPUT A$(J):J = J +1
450 GOTO 440
460 IF PEEK(222) < >5 THEN PRINT "ERROR "; PEEK(222);" OCCURRED.": END
470 POKE 216,0: REM TURN OFF ONERR GOTO
480 REM IF NOT END OF DATA, ABORT WITH ERROR NUMBER
490 NENTRIES = J -1
500 R1 = R1 -1:R2 = R1
510 REM PREPARE TO SORT
520 FOR J = 1 TO NENTRIES:B%(J) = J: NEXT J
530 PRINT D$;"CLOSE ";INNAME$
540 PRINT "SORTING";
550 REM BUBBLE SORT - SLOW BUT EASY TO WRITE
560 FOR I = 2 TO NENTRIES
570 REM DON'T NEED TO PUT AT FRONT FOR SPEED. THAT'S ONLY IMPORTANT WHEN USING GOTOS.
580 PRINT ".";
590 FOR J = NENTRIES TO I STEP -1
600 IF A$(B%(J -1)) >A$(B%(J)) THEN TEMP = B%(J -1):B%(J -1) = B%(J):B%(J) = TEMP
610 NEXT J: NEXT I
620 PRINT : PRINT D$;"OPEN ";FILE$
630 PRINT D$;"WRITE ";FILE$
640 REM WRITE OUT THE /MOVE COMMANDS
650 REM THE ESC COMMAND IN LINE 310 TELLS VISICALC NOT TO MOVE THE CURRENT
660 REM LINE BUT TO MOVE THE LINE WHOSE # FOLLOWS
670 FOR J = 1 TO NENTRIES
680 PRINT "/M";ESC$;COL$;B%(J) +R1;".";COL$;J +R2
690 FOR I = 1 TO NENTRIES: IF B%(I) <B%(J) THEN B%(I) = B%(I) +1
700 NEXT I
710 NEXT J
720 PRINT CHR$(7); CHR$(7); CHR$(7); CHR$(0); CHR$(0): REM SET UP TO RING THE BELL IN VISICALC. THE NULS ENSURE THAT VISICALC'S DOS WON'T READ BEYOND THE END OF YOUR FILE.
730 PRINT D$;"CLOSE ";FILE$
740 REM WAKE UP OPERATOR
750 FOR J = 1 TO 50:I = PEEK( -16336) + PEEK( -16336) + PEEK( -16336): NEXT J